<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-09-18 17:09:15
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\System;

use App\Http\Properties\System\UserProperties;
use App\Http\Services\Service;
use App\Repositories\DicRepository;
use App\Repositories\RoleRepository;
use App\Repositories\UserRepository;
use App\Repositories\MenuRepository;
use App\Repositories\VersionRepository;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class DicService extends Service
    {
    public function __construct(DicRepository $repository)
        {
        $this->repository = $repository;
        }

    public function getCategory()
        {
        return $this->repository->getCategory();
        }
    protected function deleteCategory($request)
        {
        return $this->repository->deleteCategory($request->input("id"));
        }
    public function list()
        {
        $request = request();
        if ($request->has("dic_category_id"))
            $this->repository->whereDicCategoryId($request->input("dic_category_id"));
        $data = $this->repository->condition($request)->getGrid();
        return $data;
        // return $this->success($data, "list success!");
        }
    public function getDicData($request)
        {
        // DB::enableQueryLog();
        return $this->repository
            ->setModel($this->repository->category())
            ->condition(request())
            ->getRelatedData("dic", [], function ($q)
                {
                $q->where("status", 1)->orderBy("sort");
                });
        // dd(DB::getQueryLog());
        }
    public function get()
        {
        return $this->repository->condition(request())->get();
        }
    protected function saveCategory($request)
        {
        return $this->repository->saveCategory($request->toArray());
        }
    protected function save($request)
        {
        $data = $request->toArray();
        $id   = isset($data['id']) ? $data['id'] : 0;
        //更新字典，更新缓存，后端刷新
        if ($id > 0)
            VersionRepository::newver();
        $gdata       = $this->repository->updateOrCreate(['id' => $id], $data);
        $gdata->code = $gdata->DicCategory->code;
        $gdata->save();
        return $gdata;
        }
    protected function delete($request)
        {
        $ids = [];
        if ($request->has("ids")) {
            $ids = $request->input("ids");
            }
        if ($request->has("id"))
            $ids[] = $request->input("id");
        return $this->repository->delete($ids);
        }
    protected function enable()
        {
        return $this->repository->enable(request()->input("id"));
        }

    public function sort()
        {

        $data = request("data");
        $sort = 0;
        foreach ($data as $v) {
            $this->repository->whereId($v['id'])->update(['sort' => $sort++]);
            }

        }

    }